package com.example.CarManagement.sys.controller;

import com.example.CarManagement.common.Result;
import com.example.CarManagement.exception.ServiceException;
import com.example.CarManagement.sys.dto.WxUserLogin;
import com.example.CarManagement.sys.entity.LoginUser;
import com.example.CarManagement.sys.entity.Role;
import com.example.CarManagement.sys.entity.User;
import com.example.CarManagement.sys.entity.UserRole;
import com.example.CarManagement.sys.mapper.RoleMapper;
import com.example.CarManagement.sys.service.impl.RoleServiceImpl;
import com.example.CarManagement.sys.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 徐佳彪
 * @since 2025-07-08
 */
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private RoleServiceImpl roleService;

    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        return userService.login(user);
    }


    @PostMapping("/wxLogin")
    public Result<?> wxLogin(@RequestBody WxUserLogin user) {
        return userService.wxLogin(user);
    }


    @GetMapping("/info")
    public Result<?> info(@RequestHeader("Authorization") String token) {
        LoginUser userInfo = userService.getUserInfo(token);
        if (userInfo != null) {
            User user = userInfo.getUser();
            user.setPassword(null);
            Role roleByUserId = roleService.getRoleByUserId(user.getId());
            userInfo.setRole(roleByUserId);
            userInfo.setUser(user);
            return Result.success(userInfo);
        } else {
            throw new ServiceException("401", "用户信息有误，请重新登录");
        }
    }


    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("Authorization") String token) {

        return userService.logout(token);
    }


    @PutMapping("/updateSelfPass")
    public Result<?> updateSelfPass(@RequestBody User user, @RequestHeader("Authorization") String token) {
        return userService.updateSelfPassword(user, token);
    }


    @PutMapping("/updateAv")
    public Result<?> updateAv(@RequestBody User user) {
        return userService.updateAv(user);
    }


    @PreAuthorize("@se.hasPermi('USER_UPDATE_PASSWORD')")
    @PostMapping("/updatePassword")
    public Result<?> updatePassword(@RequestBody User user, @RequestHeader("Authorization") String token) {
        return userService.updatePassword(user, token);
    }


    @PreAuthorize("@se.hasPermi('USER_INSERT')")
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        return userService.register(user);
    }

    @PreAuthorize("@se.hasPermi('USER_SELECT')")
    @GetMapping("/getUserOperations")
    public Result<?> getUserOperations() {
        return userService.getUserOperations();
    }


    @PreAuthorize("@se.hasPermi('USER_SELECT')")
    @GetMapping("/getUserList")
    public Result<?> getUserList(@RequestParam Map<String, Object> searchModel, @RequestHeader("Authorization") String token) {

        return userService.getUserList(searchModel, token);

    }

    @PreAuthorize("@se.hasPermi('USER_UPDATE')")
    @GetMapping("/{id}")
    public Result<?> getUserById(@PathVariable String id) {
        return Result.success(userService.getById(id));
    }


    @PreAuthorize("@se.hasPermi('USER_DELETE')")
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable String id) {
        return Result.success(userService.removeById(id));
    }


    @PreAuthorize("@se.hasPermi('USER_UPDATE')")
    @PostMapping("/update")
    public Result<?> update(@RequestBody User user) {
        return userService.updateUser(user);
    }


    /**
     * 设置用户权限
     *
     * @param user
     * @return
     */
    @PreAuthorize("@se.hasPermi('USER_UPDATE')")
    @PostMapping("/setUserRole")
    public Result<?> setUserRole(@RequestBody UserRole userRole) {
        return userService.setUserRole(userRole);
    }

    @PreAuthorize("@se.hasPermi('USER_UPDATE')")
    @GetMapping("/getUserRole/{id}")
    public Result<?> getUserRole(@PathVariable String id) {
        return userService.getUserRole(id);
    }
}
